package de.slothsoft.bookmaker.parse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

public final class ParseUtil {

    public static void parseUrl(String url, Function function) throws IOException {
        parseStream(new URL(url).openStream(), function);
    }

    public static void parseStream(InputStream stream, Function function) throws IOException {
        try (Reader in = new InputStreamReader(stream); BufferedReader reader = new BufferedReader(in);) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (!function.execute(line)) {
                    break;
                }
            }
        }
    }

    private ParseUtil() {
        // hide me just hide me here
    }

    /**
     * 
     */

    public static interface Function {

        boolean execute(String line);
    }
}
